/**
 * Created by taohj on 2014/8/14.
 */

$(document).ready(function () {


    var getDictionaryURL = '/getDictionarys';
    var getOrgURL = '/getOrgs';
    var getDataThemesURL = '/getDataThemes';
    var getAPPThemesURL = '/getAPPThemes';
    var getDatasetURL = "getDatasets";

    $("input[dataSync='true'][syncType=dictionary]").each(function () {
        var parent = $(this).parent();
        var type = $(this).attr('type')
        var dictionaryType = $(this).attr('dictionaryType');
        var name = $(this).attr('name');
        var checkedValue = $(this).attr('checkedValue');

        if (type == 'radio') {
            $.post(getDictionaryURL, {type: dictionaryType}, function (data) {
                var html = '';
                $(data.list).each(function () {
                    html += '<input type="radio" name="' + name + '" value="' + this.id + '"';
                    if (checkedValue == this.id) {
                        html += ' checked ';
                    } else if (this.defaultValue) {
                        html += ' checked ';
                    }
                    html += '>' + this.name + '&nbsp;&nbsp;'
                });
                parent.html(html);
            }, 'json');
        }
    });


    $("select[dataSync='true'][syncType=dictionary]").each(function () {


        var type = $(this).attr('type')
        var dictionaryType = $(this).attr('dictionaryType');
        var name = $(this).attr('name');
        var selectValue = $(this).attr('selectValue');
        var select = $(this);

        $.post(getDictionaryURL, {type: dictionaryType}, function (data) {
            var html = '';
            $(data.list).each(function () {
                var html = '<option  value="' + this.id + '"';
                if (selectValue == this.id) {
                    html += ' selected="selected" ';
                }
                html += '>' + this.name + '</option>';
                select.append(html);
            });
        }, 'json');

    });


    $("select[dataSync='true'][syncType=appPlatform]").each(function () {
        var dictionaryType = $(this).attr('dictionaryType');
        var selectValue = $(this).attr('selectValue');
        var select = $(this);
        $.post(getDictionaryURL, {type: dictionaryType}, function (data) {
            $(data.list).each(function () {
                var html = '<option  value="' + this.id + '"';
                if (selectValue == this.id) {
                    html += ' selected="selected" ';
                }
                html += '>' + this.name + '</option>';
                select.append(html);
            });
        }, 'json');
    });


    $("select[dataSync='true'][syncType=datatheme]").each(function () {
        var dictionaryType = $(this).attr('dictionaryType');
        var selectValue = $(this).attr('selectValue');
        var select = $(this);
        $.post(getDataThemesURL, {type: dictionaryType}, function (data) {
            $(data.list).each(function () {
                var html = '<option  value="' + this.id + '"';
                if (selectValue == this.id) {
                    html += ' selected="selected" ';
                }

                html += '>' + this.name + '</option>';
                select.append(html);
            });
        }, 'json');
    });


    $("select[dataSync='true'][syncType=apptheme]").each(function () {
        var dictionaryType = $(this).attr('dictionaryType');
        var selectValue = $(this).attr('selectValue');
        var select = $(this);
        $.post(getAPPThemesURL, {type: dictionaryType}, function (data) {
            $(data.list).each(function () {
                var html = '<option  value="' + this.id + '"';
                if (selectValue == this.id) {
                    html += ' selected="selected" ';
                }

                html += '>' + this.name + '</option>';
                select.append(html);
            });
        }, 'json');
    });


    $("select[dataSync='true'][syncType='org']").each(function () {
        var dictionaryType = $(this).attr('dictionaryType');
        var selectValue = $(this).attr('selectValue');
        var select = $(this);
        $.post(getOrgURL, {type: dictionaryType}, function (data) {
            $(data.list).each(function () {
                var html = '<option  value="' + this.id + '"';
                if (selectValue == this.id) {
                    html += ' selected="selected" ';
                }

                html += '>' + this.name + '</option>';
                select.append(html);
            });
        }, 'json');
    });


    $("select[dataSync='true'][syncType='dataset']").each(function () {

        var org = $(this).attr('org');
        var selectValue = $(this).attr('selectValue');
        var select = $(this);
        $.post(getDatasetURL, {org: org}, function (data) {
            $(data.list).each(function () {
                var html = '<option  value="' + this.id + '"';
                if (selectValue == this.id) {
                    html += ' selected="selected" ';
                }

                html += '>' + this.name + '</option>';
                select.append(html);
            });
        }, 'json');
    });


    $('.top li').on('click', function () {
        var href = $(this).attr('href');
        if (href) {
            location.href = href;
        } else {
            // alert($(this).html());
        }
    });


    $("li[href]").each(function () {
        $(this).on('click', function () {
            var href = $(this).attr("href");
            if (href) {
                var target = $(this).attr("target");
                if (target && target == '_blank') {
                    window.open(href);
                } else {
                    location.href = href;
                }

            }
        });

    });


    $("li[href]").css("cursor", "pointer")


});


$.noty.defaults = {
    layout: 'top',
    theme: 'defaultTheme',
    type: 'alert',
    text: '', // can be html or string
    dismissQueue: true, // If you want to use queue feature set this true
    template: '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
    animation: {
        open: {height: 'toggle'},
        close: {height: 'toggle'},
        easing: 'swing',
        speed: 500 // opening & closing animation speed
    },
    timeout: false, // delay for closing event. Set false for sticky notifications
    force: false, // adds notification to the beginning of queue when set to true
    modal: false,
    maxVisible: 5, // you can set max visible notification for dismissQueue true option,
    killer: false, // for close all notifications before show
    closeWith: ['click'], // ['click', 'button', 'hover']
    callback: {
        onShow: function () {
        },
        afterShow: function () {
        },
        onClose: function () {
        },
        afterClose: function () {
        }
    },
    buttons: false // an array of buttons
};